package org.uned.agonzalo16.bitacora.dao;

import static org.uned.agonzalo16.bitacora.dao.OfyFactory.ofy;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.uned.agonzalo16.bitacora.domain.User;

@Repository
public class UserDao {

	public List<User> findAll() {
		return ofy().load().type(User.class).list();
	}

	public User get(Long id) {
		return ofy().load().type(User.class).id(id).now();
	}

	public User findByUsername(String username) {
		return ofy().load().type(User.class).filter("username", username).first().now();
	}

	public User merge(User user) {
		ofy().save().entity(user).now();
		return user;
	}

	public void delete(Long id) {
		// los usuarios no se borran de la BBDD, se dejan como inactivos
		User user = get(id);
		user.setActive(false);
		merge(user);
	}
}
